<template>
  <page-header-wrapper :title="false">
    <a-card
      title="文章列表"
    >
      <a-tabs type="card" v-model="tab" @change="onChange">
        <a-tab-pane v-for="item in catOpts" :key="item.id" :tab="item.title"></a-tab-pane>
      </a-tabs>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="问题">
                  <a-auto-complete
                    v-model="queryParam['filter[title]']"
                    :data-source="searchSource"
                    placeholder="请输入问题标题"
                    @select="onSelect"
                    @search="onSearch"
                  />
                </a-form-model-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="reset">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="table-operator">
        <s-table
          bordered
          ref="table"
          size="middle"
          :row-key="(record) => record.id"
          :columns="listColumns"
          :data="listData"
        >
          <template slot="q_title" slot-scope="text,record">
            <a-button :title="text" size="small" type="link" @click="toDetail(record.id)">{{text}}</a-button>
          </template>
          <template slot="action" slot-scope="text,record">
            <a-button size="small" type="primary" @click="toDetail(record.id)">查看</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
  </page-header-wrapper>
</template>

<script>
  import { STable } from '@/components'
  import videoColumns from '@/columns/video'
  import { getCatList } from '@/api/article'
  import { getArticleList, getSearchComp } from '@/api/video'

  export default {
    components: {
      STable
    },
    name: 'ArticleList',
    data () {
      return {
        listColumns: videoColumns.listColumns,
        listData: parameters => {
          return getArticleList(Object.assign(parameters, this.queryParam)).then(res => {
            return res.result
          })
        },
        queryParam: {},
        catOpts: [],
        advanced: true,
        searchSource: [],
        tab: '1'
      }
    },
    created () {
      this.getCat()
      this.queryParam['category_id'] = '1'
      this.queryParam['filter[cat_id]'] = '1'
    },
    methods: {
      getCat () {
        getCatList().then(res => {
          if (res.status === 0) {
            this.catOpts = res.result.data
          }
        })
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      reset () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      toDetail (id) {
        this.$router.push({ name: 'ArticleDetail', params: { id } })
      },
      onSelect (value) {
        this.queryParam['title'] = value
      },
      onSearch (searchText) {
        getSearchComp(searchText).then(res => {
          this.searchSource = res.result.title_data
        })
      },
      onChange (key) {
        this.queryParam['filter[cat_id]'] = key
        this.$refs.table.refresh(true)
      }
    }
  }
</script>

<style scoped>

</style>
